﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using DDD_AOP_WCF.Domain;
using DDD_AOP_WCF.Domain.DTO;
using DDD_AOP_WCF.Domain.Products;
using DDD_AOP_WCF.ServiceContracts;
using Project.Unity;
using Domain.Core.Specification;

namespace DDD_AOP_WCF.Service.Implements
{
    public class ProductServiceImpl : IProductService
    {
        private readonly IProductRepository _productRepository;

        /// <summary>
        /// 构造方法注入
        /// </summary>
        /// <param name="productRepository"></param>
        public ProductServiceImpl(IProductRepository productRepository)
        {
            if (productRepository == (IProductRepository)null)
                throw new ArgumentException("context can't is null.");
            _productRepository = productRepository;
        }

        #region IProductService 成员
        public ProductDTO GetProductByID(int id)
        {
            Mapper.CreateMap<Product, ProductDTO>();
            return Mapper.Map<Product, ProductDTO>(_productRepository.Find(id));
        }

        public bool CreateProduct(ProductDTO productDTO)
        {
            Mapper.CreateMap<ProductDTO, Product>();
            Product product = Mapper.Map<ProductDTO, Product>(productDTO);

            //其它默认值设置
            product.UpdateDate = DateTime.Now;
            try
            {
                _productRepository.AddProduct(product);
                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }

        public List<ProductDTO> GetProduct()
        {
            Mapper.CreateMap<Product, ProductDTO>();
            List<ProductDTO> arr = new List<ProductDTO>();
            _productRepository.GetProduct().ToList().ForEach(i =>
            {
                arr.Add(Mapper.Map<Product, ProductDTO>(i));
            });
            return arr;
        }

        public bool ModifyProduct(ProductDTO productDTO)
        {
            try
            {
                _productRepository.ModifyProduct(i => new Product
                {
                    ProductID = productDTO.ProductID,
                    ProductName = productDTO.ProductName,
                    Discount = productDTO.Discount,
                    UpdateDate = DateTime.Now,
                    SalePrice = productDTO.SaleCount,
                });


                return true;
            }
            catch (Exception)
            {
                return false;
            }


        }

        public List<ProductDTO> GetProductByName(string productName)
        {

            Mapper.CreateMap<Product, ProductDTO>();
            List<ProductDTO> arr = new List<ProductDTO>();
            return Mapper.Map<List<Product>, List<ProductDTO>>(_productRepository
                .GetProduct()
                .Where(i => i.ProductName.Contains(productName))
                .ToList());
        }

        public bool DelProduct(int id)
        {

            try
            {
                _productRepository.Delete(_productRepository.Find(id));
                return true;
            }
            catch (Exception)
            {

                return false;
            }

        }

        #endregion
    }
}